package com.xj0927.cloud.filter;


import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author: xj0927
 * @Description:
 * @Date Created in 2020-10-30 15:51
 * @Modified By:
 */
@Component //注入容器
@Slf4j
public class MyLogGatewayFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("****come in filter");
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        if(uname == null){ //如果不符合规则
            log.info("非法");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); //返回一个不被接受的状态码
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
